﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using QuickDev.Licensing;

namespace QuickDev.Licensing.Web
{
    [LicenseProvider( typeof( AppLicenseProvider ) )]
    public class HanpaiMasterPage : System.Web.UI.MasterPage , ILicenseProvider , IDisposable
    {
        private AppLicense license;

        public HanpaiMasterPage()
        {
            if ( !Assembly.GetCallingAssembly().FullName.StartsWith( "mscorlib" ) )
            {
                try
                {
                    this.license = LicenseManager.Validate( typeof( QuickDev.Licensing.Web.HanpaiMasterPage ) , this ) as AppLicense;
                }
                catch ( LicenseException )
                {
                    this.license = null;
                }
            }
        }

        private void AddGenerator( string buildNumber )
        {
            HtmlMeta child = new HtmlMeta();
            child.Attributes.Add( "name" , "generator" );
            child.Attributes.Add( "content" , string.Format( "Powered By 汉派科技 - 进销存管理系统 , Build:{0}, http://www.hanpai.com/" , buildNumber ) );
            this.Page.Header.Controls.Add( child );
        }

        public override void Dispose()
        {
            if ( this.license != null )
            {
                this.license.Dispose();
                this.license = null;
            }
            base.Dispose();
        }

        protected override void Render( HtmlTextWriter writer )
        {
            try
            {
                string buildNumber = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                this.AddGenerator( buildNumber );
                //if ( this.license == null )
                //{
                PlaceHolder holder = null;
                if ( base.Master != null )
                {
                    holder = base.Master.FindControl( "phFooter" ) as PlaceHolder;
                }
                else
                {
                    holder = this.FindControl( "phFooter" ) as PlaceHolder;
                }
                if ( holder == null )
                {
                    throw new LicenseException( typeof( QuickDev.Licensing.Web.HanpaiMasterPage ) , this , "Invalid License." );
                }
                HyperLink child = new HyperLink();
                //child.Text = string.Format( "Powered by 汉派科技 - 进销存管理系统 , Build:{0}" , buildNumber );
                child.Text = "返回网站首页";
                //child.NavigateUrl = "http://www.hanpai.com/";
                child.CssClass = "smallright";
                holder.Controls.Clear();
                holder.Controls.Add( child );
                holder.Controls.Add( new LiteralControl( String.Format( "&nbsp;Copyright © 南京汉派科技有限公司 - 进销存管理系统 , {0}" , ( DateTime.Today.Date.Year == 2009 ) ? "2009" : String.Format( "2009 - {0}" , DateTime.Today.Date.Year.ToString() ) ) ) );
                holder.Controls.Add( new LiteralControl( "<!-- Powered by Freecin Information Technology Co., Ltd -->" ) );
                holder.Visible = true;
                //}
                base.Render( writer );
            }
            catch ( Exception exception )
            {
                Console.WriteLine( exception.StackTrace );
            }
        }

        public string PublicKey
        {
            get
            {
                return resources.PublicKey;
            }
        }
    }
}

